package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.SysJob;
import com.base.vistter.iframe.entity.SysNotice;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysNoticeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/notice")
public class SysNoticeController {

    @Resource(name = "sysNoticeService")
    private SysNoticeService sysNoticeService;

    @GetMapping
    public Result<Page<SysNotice>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String title, Integer status) throws IframeException {
        Page<SysNotice> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<SysNotice> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(title)) {
            lambdaQueryWrapper.like(SysNotice::getTitle, title);
        }
        if (null != status) {
            lambdaQueryWrapper.eq(SysNotice::getStatus, status);
        }
        lambdaQueryWrapper.eq(SysNotice::getCreator, SystemContextHolder.getSessionContext().getUserId());
        lambdaQueryWrapper.orderByDesc(SysNotice::getCreateDate);
        return Result.ok(sysNoticeService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysNotice> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysNoticeService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysNotice sysNotice) throws IframeException {
        return Result.ok(sysNoticeService.save(sysNotice));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysNotice sysNotice) throws IframeException {
        return Result.ok(sysNoticeService.update(sysNotice));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysNoticeService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id) throws IframeException {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setId(id);
        sysNotice.setStatus(1);
        sysNotice.setRevocationDate(new Date());
        return Result.ok(sysNoticeService.updateById(sysNotice));
    }

    @GetMapping("/submit/approval")
    public Result<Boolean> submitApproval(@RequestParam("id") Integer id) throws IframeException {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setId(id);
        sysNotice.setStatus(3);
        return Result.ok(sysNoticeService.updateById(sysNotice));
    }

    @GetMapping("/unread")
    public Result<List<SysNotice>> unread() throws IframeException {
        return Result.ok(sysNoticeService.unreadNotice());
    }
}
